﻿///<reference path="../../../Scripts/jquery-1.4.1-vsdoc.js"/>
///<reference path="../utils/Utils.js"/>
///<reference path="../utils/Dictionary.js"/>
///<reference path="../pugins/jquery.history.js"/>

function Command(hash, callback) {
    this.Hash = hash;
    this.Callback = callback;

    this.Execute = function () {
        this.Callback.call(this, hash);
    }
}


function CommandProcessor() {
    var commands = new Dictionary();
    var _onCompletedCallback;

    this.Init = function (initCallBack) {
        $.history.init(function (hash) {
            if (hash == "") {
                if (initCallBack != undefined)
                    initCallBack.call();
            } else {
                ExecuteCommand(hash);
            }
        }, { unescape: ",/" });
    }

    this.RegisterCommand = function (hash, callback) {
        commands.Add(hash, new Command(hash, callback));
    }

    this.SetHash = function (hash, onCompletedCallback) {
        _onCompletedCallback = onCompletedCallback;
        $.history.load(hash);
    }

    function ExecuteCommand(hash) {
        var command = commands.Lookup(hash);
        if (command == undefined) {
            appPointer.Logger.LogException(hash + " hash is not registered in CommandProcessor.");
            return undefined;
        }
        command.Execute();


        if (_onCompletedCallback != undefined) {
            _onCompletedCallback.call(this, hash);
            _onCompletedCallback = undefined;
        }
    }    
}
